﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using SmartSpace.Web;

namespace SmartSpace.WebApp.Modules.Workflow.Pages
{
    public partial class User_Picker : System.Web.UI.Page
    {
        public Guid WorkflowId { get; private set; }
        public Guid ActivityId { get; private set; }
        public string Module { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            WorkflowId = RequestUtility.GetGuid(Request, "wfid");
            ActivityId = RequestUtility.GetGuid(Request, "actid");
            Module = RequestUtility.GetString(Request, "module");

            if (!IsPostBack)
            {
                // 获取用户
                ICoreService coreService = ServiceLocator.Get<ICoreService>();
                //ListResult<Role> roles = coreService.GetRoles(Module, ActivityId);
                PagedList<User> users = coreService.GetUsers(0, 999);

                this._ctlList.DataSource = users;
                this._ctlList.DataBind();
            }
        }

        protected void _ctlBtnOK_Click(object sender, EventArgs e)
        {
            StringBuilder builder = new StringBuilder();
            bool isFirst = true;
            foreach (RepeaterItem item in _ctlList.Items)
            {
                CheckBox checkBox = (CheckBox)item.FindControl("_ctlCheck");
                if (checkBox != null && checkBox.Checked)
                {
                    HiddenField hiddenValue = (HiddenField)item.FindControl("_ctlHiddenValue");
                    HiddenField hiddenName = (HiddenField)item.FindControl("_ctlHiddenName");


                    if (!isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        builder.Append(",");
                    }
                    builder.Append("{");
                    builder.AppendFormat("name:'{0}',value:'{1}'", hiddenName.Value, hiddenValue.Value);
                    builder.Append("}");
                }
            }

            Response.Write("<script>window.returnValue=[" + builder.ToString() + "];window.close();</script>");
        }
    }
}